package com.google.code.jmarias.client.phases;

import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.GameLocals;
import com.google.code.jmarias.client.components.ConfirmPickedComponent;
import com.google.code.jmarias.client.components.GrabCardsComponent;
import com.google.code.jmarias.client.components.PickCardComponent;
import com.google.code.jmarias.client.tcp.RemoteServer;
import com.google.code.jmarias.common.Card;
import com.google.code.jmarias.common.action.DropTalonAction;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Rozda vsech dvanact karet volicimu hraci, necha ho zvolit talon a nakonec se ho jeste zepta co se bude hrat.
 * @author bart
 */
public class DropTalonPhase extends AbstractPhase {

    private ArrayList<Vector2f> cardsPositions = new ArrayList<Vector2f>();
    
    public DropTalonPhase(RemoteServer server, Globals globals, GameLocals locals) throws SlickException {
        this.server = server;
        this.globals = globals;
        this.locals = locals;
        
        cardsPositions = locals.getPositionsInHand(locals.getCardsEntities().size(), locals.getHandPosition());

        serialComponents = new LinkedList();

        // Naplneni fronty seriovyych komponent tak, jak maji nasledovat po sobe
        this.addSerialComponent(new PickCardComponent(0, globals, locals, cardsPositions, locals.getCardsEntities()));
        this.addSerialComponent(new PickCardComponent(0, globals, locals, cardsPositions, locals.getCardsEntities()));
        this.addSerialComponent(new ConfirmPickedComponent(0, globals, locals, locals.getCardsEntities()));
        this.addSerialComponent(new GrabCardsComponent(0, cardsPositions, locals.getCardsEntities()));

        locals.getHandEntity().AddComponent(serialComponents.remove());
        
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
           if(locals.getHandEntity().getComponent(0) == null) {

               if(serialComponents.size() > 0) {
                   System.out.println("Pridavam " + serialComponents.peek());
                   locals.getHandEntity().AddComponent(serialComponents.remove());
               } else {
                   // Nezbyly zadne komponenty k zpracovani - nastava konec faze.
                   locals.setCurrentPhase(null);
                   // Posilam serveru vybranou kartu
                   Card pickedCards[] = { locals.getPickedCards().get(0).getCardObject(), locals.getPickedCards().get(1).getCardObject() };
                   System.out.println("Posilam serveru vybranou kartu: " + pickedCards);
//                   server.sendAction(new SendTalonAction(pickedCards));
                   server.sendAction(new DropTalonAction(pickedCards));
                   locals.getPickedCards().clear();
                   return;
               }
           }

            locals.getHandEntity().update(gc, null, delta);
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) throws SlickException {
           locals.getHandEntity().render(gc, sb, gr);
    }

    @Override
    public void refusePickedCards() {
        serialComponents.clear();
        locals.getPickedCards().clear();
        try {
            this.addSerialComponent(new PickCardComponent(0, globals, locals, cardsPositions, locals.getCardsEntities()));
            this.addSerialComponent(new PickCardComponent(0, globals, locals, cardsPositions, locals.getCardsEntities()));
            this.addSerialComponent(new ConfirmPickedComponent(0, globals, locals, locals.getCardsEntities()));
            this.addSerialComponent(new GrabCardsComponent(0, cardsPositions, locals.getCardsEntities()));
        } catch (SlickException ex) {
            Logger.getLogger(PickTrumpPhase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
